package net.dodoman.thread;

public class SyncMain {

	int i = 0;

	synchronized public void sync() {
		i++;
	}

	public static void main(String[] args) {
		SyncMain syncMain = new SyncMain();
		SyncThread st = new SyncThread(syncMain);
		SyncThread1 st1 = new SyncThread1(syncMain);
	}
}

class SyncThread extends Thread {
	SyncMain syncMain;

	public SyncThread(SyncMain syncMain) {
		start();
		this.syncMain = syncMain;
	}

	public void run() {
		while (true) {
			int count = 0;
			syncMain.sync();
			count = syncMain.i;
			System.out.println(count);
			synchronized (syncMain) {
				try {
					syncMain.wait();
				} catch (InterruptedException e) {
					throw new RuntimeException();
				}
			}
			if (count > 10) {
				//interrupt();
				break;
			}
		}
	}
}

class SyncThread1 extends Thread {
	SyncMain syncMain;

	public SyncThread1(SyncMain syncMain) {
		start();
		this.syncMain = syncMain;
	}

	public void run() {
		while (true) {
			int count = 0;
			syncMain.sync();
			count = syncMain.i;
			System.out.println(count);
			synchronized (syncMain) {
				syncMain.notify();
			}
			if (count > 10) {
				//interrupt();
				break;
			}
		}
	}
}